<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MX_Controller {
  function __construct() {
    $this->load->model('login/mlogin');
    $this->load->library('encrypt');
//    $this->load->library('form_eksternal');
//    $this->load->library('global_models');
//    $this->load->library('nbscache');
//    $this->load->library('parser');
//    $this->load->library('form_validation');
//    $this->load->helper('path');
//    $this->load->helper('url');
    $this->load->library('session');
//    $this->session->sess_destroy();
  }
  
  public function login_zikula(){
    $this->load->library('global_models');
    $this->global_models->get_connect("zikula");
    
    $pn_gid = $this->global_models->get_field("academicportal_group_membership", "pn_gid", array("pn_uid" => $this->input->post("authid")));
    $users = $this->global_models->get("academicportal_users", array("pn_uid" => $this->input->post("authid"), "pn_pass" => $this->input->post("pn_pass")));
    
    $this->global_models->get_connect("default");
    if($users){
      $this->global_models->get_connect("zikula");
      $group_zikula = $this->global_models->get_query("
        SELECT B.*
        FROM academicportal_group_membership AS A
        LEFT JOIN academicportal_groups AS B ON A.pn_gid = B.pn_gid
        WHERE A.pn_uid = {$users[0]->pn_uid}
        ");
      $this->global_models->get_connect("default");
      $keuangan_mahasiswa_setting_cms_group_temp = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_setting_cms_group");
      $keuangan_mahasiswa_setting_cms_group = unserialize($keuangan_mahasiswa_setting_cms_group_temp[1]);
      $privilege_login = array_search($group_zikula[0]->pn_gid, $keuangan_mahasiswa_setting_cms_group);
          if($group_zikula[1]->pn_gid)
            $privilege_login = array_search($group_zikula[1]->pn_gid, $keuangan_mahasiswa_setting_cms_group);
      $privilege_nubusa = $this->nbscache->get_explode("variable", $privilege_login."2");

      $privilege_detail_nubusa = $this->global_models->get("m_privilege", array("id_privilege" => $privilege_nubusa[1]));
  //          if($this->input->post()){
  //            $this->debug($privilege_detail_nubusa, true);
  //          }
  //          $this->global_models->get_connect("zikula");

      if($group_zikula){
        $newdata = array(
          'name'  => $users[0]->pn_uname,
          'ename'  => substr(md5(date("d")), 0, 5).$this->encrypt->encode($users[0]->pn_uname),
          'epass'  => substr(md5(date("d")), -5).$users_zikula[0]->pn_pass,
          'email'     => $users[0]->pn_email,
          'id'     => $users[0]->pn_uid,
  //              'privilege'     => $privilege_detail_nubusa[0]->id_user_privilege,
          'id_privilege'     => $privilege_detail_nubusa[0]->id_privilege,
          'dashbord'     => $privilege_detail_nubusa[0]->dashbord,
          'logged_in' => TRUE
        );

        $this->session->set_userdata($newdata);

      }
    }
    redirect();
//    
//    $this->debug($this->global_models->get("academicportal_sf_rekening_bank"));
//    
//    $this->debug($this->global_models->get("m_users"));
//    $this->debug($this->input->post(NULL), true);
  }
  
	public function index($pesan = "hal"){
//    $this->debug($_SERVER, true);
    $config = array(
      array(
            'field'   => 'memuname', 
            'label'   => 'Username', 
            'rules'   => 'required'
         ),
      array(
            'field'   => 'mempass', 
            'label'   => 'Password', 
            'rules'   => 'required'
         ),
    );
    
    $this->form_validation->set_rules($config);
    $this->template->title('Login', "Sistem");
    if ($this->form_validation->run() == FALSE){
      $this->session->sess_destroy();
//      print "gagal";
			$this->template->build('main', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'field'   => array('memuname' => $this->input->post('memuname'))
            ));
      $this->template
        ->set_layout('login')
        ->build('main');
		}
		else{
      $cek_login = $this->mlogin->cek_login($this->input->post('memuname'), $this->input->post('mempass'));
      if($cek_login === true){
        redirect($this->session->userdata('dashbord'));
      }
      if($cek_login !== true){
        
//        print 'sasa';
//        die;
//        cek login zikula
        $this->global_models->get_connect("zikula");
        
        $users_zikula = $this->global_models->get_query("
          SELECT *
          FROM academicportal_users
          WHERE pn_uname = '{$this->input->post('memuname')}' AND
            pn_pass = '".md5($this->input->post('mempass'))."'
          ");
        
        if($users_zikula){
          $group_zikula = $this->global_models->get_query("
            SELECT B.*
            FROM academicportal_group_membership AS A
            LEFT JOIN academicportal_groups AS B ON A.pn_gid = B.pn_gid
            WHERE A.pn_uid = {$users_zikula[0]->pn_uid}
            ");
          
          $this->global_models->get_connect("default");
          $keuangan_mahasiswa_setting_cms_group_temp = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_setting_cms_group");
          $keuangan_mahasiswa_setting_cms_group = unserialize($keuangan_mahasiswa_setting_cms_group_temp[1]);
          $privilege_login = array_search($group_zikula[0]->pn_gid, $keuangan_mahasiswa_setting_cms_group);
          if($group_zikula[1]->pn_gid)
            $privilege_login = array_search($group_zikula[1]->pn_gid, $keuangan_mahasiswa_setting_cms_group);
          $privilege_nubusa = $this->nbscache->get_explode("variable", $privilege_login."2");
          
          $privilege_detail_nubusa = $this->global_models->get("m_privilege", array("id_privilege" => $privilege_nubusa[1]));
//          if($this->input->post()){
//            $this->debug($privilege_detail_nubusa, true);
//          }
//          $this->global_models->get_connect("zikula");
//          $this->debug($group_zikula);
//          $this->debug($keuangan_mahasiswa_setting_cms_group, true);
          if($group_zikula AND $privilege_detail_nubusa[0]->id_privilege){
            $newdata = array(
              'name'  => $users_zikula[0]->pn_uname,
              'ename'  => substr(md5(date("d")), 0, 5).$this->encrypt->encode($users_zikula[0]->pn_uname),
              'epass'  => substr(md5(date("d")), -5).$users_zikula[0]->pn_pass,
              'email'     => $users_zikula[0]->pn_email,
              'id'     => $users_zikula[0]->pn_uid,
//              'privilege'     => $privilege_detail_nubusa[0]->id_user_privilege,
              'id_privilege'     => $privilege_detail_nubusa[0]->id_privilege,
              'dashbord'     => $privilege_detail_nubusa[0]->dashbord,
              'logged_in' => TRUE
            );
            
            $this->session->set_userdata($newdata);
            
          }
        }
//        $this->global_models->get_connect("default");
//        if($users_zikula){
//          
//        }
//        else{
//          $this->global_models->get_connect("default");
//          redirect($this->session->userdata('dashbord'));
//        }
        $this->global_models->get_connect("default");
        if($this->session->userdata('logged_in')){
          redirect($this->session->userdata('dashbord'));
        }
        else{
          $this->session->sess_destroy();
          $this->template->build('main', 
          array('message' => "gagal",
                'url'     => base_url()."themes/srabon/",
                'field'   => array('memuname' => $this->input->post('memuname'))
              ));
          $this->template
            ->set_layout('login')
            ->build('main');
        }
      }
      
//      else{
//        $this->session->sess_destroy();
//        $this->template->build('main', 
//        array('message' => "gagal",
//              'url'     => base_url()."themes/srabon/",
//              'field'   => array('memuname' => $this->input->post('memuname'))
//            ));
//        $this->template
//          ->set_layout('login')
//          ->build('main');
//      }
		}
	}
  function forgot_password(){
    if($this->input->post()){
      $id_users = $this->global_models->get_field("m_users", "id_users", array("email" => $this->input->post("email")));
      if($id_users){
        $new_password = random_string('alnum',8);
        $enpass = $this->encrypt->encode($new_password);
        $email_user = $this->global_models->get("m_users", array("id_users" => $id_users));
        $this->global_models->update('m_users', array("id_users" => $id_users),array('pass' => $enpass));

        //kirim email
        $this->email->from('no-reply@technomotor.co.id', APPNAME);
        $this->email->to($email_user[0]->email);

        $this->email->subject('Password Reset');
        $this->email->message("
          Your new password 
          link => ".base_url()."
          user => {$email_user[0]->name}
          pass => {$new_password}
          ");

        if($this->email->send() === TRUE){
          $pesan = "New Password has been send to your mail";
        }
        else{
          $pesan = "Password has been changed, but the email delivery fails. Please contact your admin";
        }
      }
      else{
        $pesan = "Your e-mail has not been registered";
      }
    }
    
    $this->template->build('forgot-password', 
    array(
          'url'         => base_url()."themes/".DEFAULTTHEMES."/",
          'pesan'       => $pesan
        ));
    $this->template
      ->set_layout('login')
      ->build('forgot-password');
  }
}